<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>文章列表</span>
        <el-button type="primary" @click="showDialog">添加分类</el-button>
      </div>
      <el-table :data="list" style="width: 100%" stripe border>
        <el-table-column prop="id" label="ID" width="150"> </el-table-column>
        <el-table-column prop="cate_name" label="分类名称"> </el-table-column>
        <el-table-column prop="cate_alias" label="分类别名"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="showeideDialog(scope.row)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="delList(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加对话框 -->
    <el-dialog title="添加文章" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="addData" :rules="rules" label-width="80px" ref="form">
        <el-form-item prop="cate_name" label="分类名称">
          <el-input v-model="addData.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="cate_alias" label="分类别名">
          <el-input v-model="addData.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="hideDialog">取 消</el-button>
        <el-button type="primary" @click="addList">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑的对话框 -->
    <el-dialog title="修改文章" :visible.sync="editDialog" width="500px">
      <el-form :model="editData" :rules="rules" label-width="80px" ref="editForm">
        <el-form-item prop="cate_name" label="分类名称">
          <el-input v-model="editData.cate_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="cate_alias" label="分类别名">
          <el-input v-model="editData.cate_alias" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="hideEditDialog">取 消</el-button>
        <el-button type="primary" @click="editCategory">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getCategoryAPI, addCategoryAPI, delCategoryAPI, editCategoryAPI } from '@/api/category'
export default {
  data() {
    return {
      // 页面列表数据
      list: [],
      // 控制添加的对话框
      dialogFormVisible: false,
      // 控制编辑的对话框
      editDialog: false,
      // 添加的数据项
      addData: {
        cate_name: '',
        cate_alias: ''
      },
      // 编辑的数据项
      editData: {},
      // 验证规则
      rules: {
        cate_name: [
          { required: true, message: '分类名称必填', trigger: 'blur' },
          { min: 1, max: 10, message: '长度1~10位', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '分类名称必填', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,10}$/,
            message: '长度1~10位,且只能为英文或数字',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // 获取文章分类的方法
    async getCategory() {
      const { data: res } = await getCategoryAPI()
      this.list = res.data
      // console.log(this.list)
    },
    // 添加分类方法
    addList() {
      this.$refs.form.validate(async valid => {
        if (!valid) return
        const { data: res } = await addCategoryAPI(this.addData)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.dialogFormVisible = false
          this.getCategory()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 删除分类的方法
    delList(id) {
      if (id === 1 || id === 2) {
        this.$message.error('不允许删除前两条数据')
      } else {
        this.$confirm('确定要删除这条数据吗？', '提示', {
          type: 'warning',
          callback: async r => {
            if (r === 'confirm') {
              // console.log(id)
              const { data: res } = await delCategoryAPI(id)
              if (res.code === 0) {
                this.$message.success(res.message)
                this.getCategory()
              } else {
                this.$message.error(res.message)
              }
            }
          }
        })
      }
    },
    // 编辑分类的方法
    editCategory() {
      this.$refs.editForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await editCategoryAPI(this.editData)
        if (res.code === 0) {
          this.$message.success(res.message)
          this.editDialog = false
          this.getCategory()
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // 展示添加的对话框
    showDialog() {
      this.dialogFormVisible = true
    },
    // 展示编辑的对话框
    showeideDialog(row) {
      if (row.id === 1 || row.id === 2) {
        this.$message.error('不允许修改前两条数据')
      } else {
        this.editData = { ...row } // 把row拷贝到data中
        this.editDialog = true
      }
    },
    // 隐藏添加的对话框
    hideDialog() {
      this.$refs.form.resetFields() // 重置表单
      this.dialogFormVisible = false
    },
    // 隐藏编辑的对话框
    hideEditDialog() {
      this.editDialog = false
    }
  },
  created() {
    // 页面刷新调用获取文章分类的方法
    this.getCategory()
  }
}
</script>
<style>
.clearfix {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
